import { io, Socket } from "socket.io-client";
import {
  CurveMeshOptions,
  MODEL_TYPE,
  SurfaceMeshOptions,
  SelectedParts,
  API_EVENT,
  IRes,
  IReq,
  AnalysisOptions,
  EdgePartitionOptions,
} from "../interface";
import { v4 as uuidv4 } from "uuid";
import { devtool } from "cax-base";
import { FrontProcessing } from "@/FrontProcessing";
import { System } from "@/FrontProcessing/System/System";
import { STATE_EVENT } from "@/FrontProcessing/State/State";
import { connect } from "./connect";
import { add_entity } from "./add_entity";
import { delete_entity } from "./delete_entity";
import { file_load } from "./file_load";
import { file_save } from "./file_save";
import { model_save } from "./model_save";
import { mesh_create } from "./mesh_create";
import { measure_point_to_point } from "./measure_point_to_point";
import { model_info } from "./model_info";
import { project_save } from "./project_save";
import { project_load } from "./project_load";

export class Connector {
  client: Socket | null = null;
  system: System;

  constructor(system: System) {
    this.system = system;
    this.register("connect", connect);
    this.register("add_entity", add_entity);
    this.register("delete_entity", delete_entity);
    this.register("file_load", file_load);
    this.register("file_save", file_save);
    this.register("model_save", model_save);
    this.register("mesh_create", mesh_create);
    this.register("measure_point_to_point", measure_point_to_point);
    this.register("model_info", model_info);
    this.register("project_save", project_save);
    this.register("project_load", project_load);
  }

  register(funcName: string, func: any) {
    (this as any)[funcName] = func.bind(this);
    return this;
  }

  // Ⓜ️
  mid_surface(options: SurfaceMeshOptions): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MID_SURFACE,
      // todo 后续需要传入类型
      Params: options,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        console.log("-----ev-----", ev);
        resolve(ev.Result);
      });
    });
  }
  edge_partition(options: EdgePartitionOptions): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.Edge_Partition,
      // todo 后续需要传入类型
      Params: options,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        console.log("-----ev-----", ev);
        resolve(ev.Result);
      });
    });
  }

  // Ⓜ️
  analysis_hole(options: AnalysisOptions): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.ANALYSIS_HOLE,
      // todo 后续需要传入类型
      Params: options,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        console.log("-----ev-----", ev);
        resolve(ev.Result);
      });
    });
  }

  face_fillet(parts: SelectedParts[]): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.FACE_FILLET,
      // todo 后续需要传入类型
      Params: {
        parts,
        type: 2,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        console.log("-----ev-----", ev);
        resolve(ev.Result);
      });
    });
  }

  counter_bore(parts: SelectedParts[]): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.COUNTER_BORE,
      // todo 后续需要传入类型
      Params: {
        parts,
        type: 2,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        console.log("-----ev-----", ev);
        resolve(ev.Result);
      });
    });
  }

  entity_delete() {
    return true;
  }

  delete_mesh(EntityId: number): Promise<boolean> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.DELETE_MESH,
      // todo 后续需要传入类型
      Params: {
        EntityId,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        devtool.success("delete_mesh", ev);
        if (ev.Status === 0) {
          resolve(true);
        } else {
          reject(false);
        }
      });
    });
  }

  mesh_remesh(
    EntityId: number,
    MeshIds: Array<number>,
    MeshSize: number,
    EleType: string
  ): Promise<string> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MESH_REMESH,
      // todo 后续需要传入类型
      Params: {
        EntityId,
        MeshIds,
        MeshSize,
        EleType,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        devtool.success("mesh_remesh", ev);
        if (ev.Status === 0) {
          resolve(ev.Result.FilePath);
        } else {
          reject(ev.Errmsg);
        }
      });
    });
  }

  mask(FaceId: number): Promise<string> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MASK,
      // todo 后续需要传入类型
      Params: {
        FaceId,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        devtool.success("mask", ev);
        if (ev.Status === 0) {
          resolve(ev.Result.FilePath);
        } else {
          reject(ev.Errmsg);
        }
      });
    });
  }

  unmaskAll(): Promise<string> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.UNMASK_ALL,
      // todo 后续需要传入类型
      Params: {},
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        devtool.success("unmask all", ev);
        if (ev.Status === 0) {
          resolve(ev.Result.FilePath);
        } else {
          reject(ev.Errmsg);
        }
      });
    });
  }

  reverseAll(): Promise<string> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.REVERSE_ALL,
      // todo 后续需要传入类型
      Params: {},
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        devtool.success("reverse all", ev);
        if (ev.Status === 0) {
          resolve(ev.Result.FilePath);
        } else {
          reject(ev.Errmsg);
        }
      });
    });
  }

  reverse(): Promise<string> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.REVERSE,
      // todo 后续需要传入类型
      Params: {},
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        devtool.success("reverse", ev);
        if (ev.Status === 0) {
          resolve(ev.Result.FilePath);
        } else {
          reject(ev.Errmsg);
        }
      });
    });
  }

  create_load_constraints(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CREATE_LOAD_CONSTRAINTS,
      Params: values,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  create_bcs(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CREATE_BCS,
      Params: values,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  delete_force_set(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    let Type = API_EVENT.DELETE_FORCE_SET;
    if (values.type === "force_set") {
      Type = API_EVENT.DELETE_FORCE_SET;
    } else if (values.type === "displacement_set") {
      Type = API_EVENT.DELETE_DISPLACEMENT_SET;
    }
    const req: IReq = {
      Topic: "api",
      Type,
      Params: values,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  load_constraint_socket(values: any, api_event: API_EVENT) {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: api_event,
      Params: values,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  delete_item(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    let Type = API_EVENT.DELETE_ITEM_FORCE;
    if (values.type === "force_set") {
      Type = API_EVENT.DELETE_ITEM_FORCE;
    } else if (values.type === "displacement_set") {
      Type = API_EVENT.DELETE_ITEM_DISPLACEMENT;
    }
    const req: IReq = {
      Topic: "api",
      Type,
      Params: values,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  create_displacement(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CREATE_DISPLANCEMENT,
      Params: values,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  create_force(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CREATE_FORCE,
      Params: values,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  modify_force(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MODIFY_FORCE,
      Params: values,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  get_force_info(id: string): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.GET_FORCE_INFO,
      Params: id,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  modify_displacement(value: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MODIFY_DISPLACEMENT,
      Params: value,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  get_displacement_info(id: string): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.GET_DISPLACEMENT_INFO,
      Params: id,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  get_all_load_constraints(): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.GET_LOAD_CONSTRAINTS_ALL,
      Params: {},
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  get_all_materials(): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MATERIALS_ALL,
      Params: {},
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  create_material(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MATERIAL_CREATE,
      Params: {
        values,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) reject(ev.Errmsg);
        resolve(ev.Result);
      });
    });
  }

  delete_material(id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MATERIAL_DELETE,
      Params: {
        id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  get_material_by_id(id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MATERIAL_ONE,
      Params: {
        id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  modify_material(id: number, new_values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MATERIAL_MODIFY,
      Params: {
        id,
        new_values,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) {
          reject(ev.Errmsg);
        }
        resolve(ev.Result);
      });
    });
  }

  get_all_properties(): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.PROPERTIES_ALL,
      Params: {},
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  create_property(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.PROPERTY_CREATE,
      Params: {
        values,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) reject(ev.Errmsg);
        resolve(ev.Result);
      });
    });
  }

  delete_property(id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.PROPERTY_DELETE,
      Params: {
        id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  get_property_by_id(id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.PROPERTY_ONE,
      Params: {
        id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  modify_property(id: number, new_values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.PROPERTY_MODIFY,
      Params: {
        id,
        new_values,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) {
          reject(ev.Errmsg);
        }
        resolve(ev.Result);
      });
    });
  }

  assign_property(entity_ids: number[], prop_id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.PROPERTY_ASSIGN,
      Params: {
        entity_ids,
        prop_id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) {
          reject(ev.Errmsg);
        }
        resolve(ev.Result);
      });
    });
  }

  get_all_outputblocks(): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.OUTPUTBLOCKS_ALL,
      Params: {},
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  create_outputblock(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.OUTPUTBLOCK_CREATE,
      Params: {
        values,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) reject(ev.Errmsg);
        resolve(ev.Result);
      });
    });
  }

  delete_outputblock(id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.OUTPUTBLOCK_DELETE,
      Params: {
        id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  get_outputblock_by_id(id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.OUTPUTBLOCK_ONE,
      Params: {
        id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  modify_outputblock(id: number, new_values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.OUTPUTBLOCK_MODIFY,
      Params: {
        id,
        new_values,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) {
          reject(ev.Errmsg);
        }
        resolve(ev.Result);
      });
    });
  }

  assign_outputblock(entity_ids: number[], prop_id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.OUTPUTBLOCK_ASSIGN,
      Params: {
        entity_ids,
        prop_id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) {
          reject(ev.Errmsg);
        }
        resolve(ev.Result);
      });
    });
  }

  get_all_cards(): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CARDS_ALL,
      Params: {},
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  create_card(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CARD_CREATE,
      Params: {
        values,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) reject(ev.Errmsg);
        resolve(ev.Result);
      });
    });
  }

  delete_card(id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CARD_DELETE,
      Params: {
        id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  get_card_by_id(id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CARD_ONE,
      Params: {
        id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  modify_card(id: number, new_values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CARD_MODIFY,
      Params: {
        id,
        new_values,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) {
          reject(ev.Errmsg);
        }
        resolve(ev.Result);
      });
    });
  }

  assign_card(entity_ids: number[], prop_id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CARD_ASSIGN,
      Params: {
        entity_ids,
        prop_id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) {
          reject(ev.Errmsg);
        }
        resolve(ev.Result);
      });
    });
  }

  get_all_coordinates(): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.COORDINATES_ALL,
      Params: {},
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  create_coordinate(values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.COORDINATE_CREATE,
      Params: {
        values,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) reject(ev.Errmsg);
        resolve(ev.Result);
      });
    });
  }

  delete_coordinate(id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.COORDINATE_DELETE,
      Params: {
        id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  get_coordinate_by_id(id: number): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.COORDINATE_ONE,
      Params: {
        id,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        resolve(ev.Result);
      });
    });
  }

  modify_coordinate(
    nodes_info: any,
    name: string,
    setName: string
  ): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.COORDINATE_MODIFY,
      Params: {
        nodes_info,
        name,
        setName,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) {
          reject(ev.Errmsg);
        }
        resolve(ev.Result);
      });
    });
  }
  disconnect() {
    if (this.client == null) return Promise.reject("socket未初始化");
    this.client?.emit("close", 1);
  }

  cell_create(data: any): Promise<boolean> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.CREATE_CELL,
      // todo 后续需要传入类型
      Params: data,
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status === 0) {
          resolve(ev.Result.FilePath);
        } else {
          reject(ev.Errmsg);
        }
      });
    });
  }
  mesh_split(this: Connector, values: any): Promise<any> {
    if (this.client == null) return Promise.reject("socket未初始化");
    const req: IReq = {
      Topic: "api",
      Type: API_EVENT.MESH_SPLIT,
      Params: {
        values,
      },
      Req: 1,
      Res: 0,
      Tag: uuidv4(),
    };
    this.client.emit("api", req);
    return new Promise((resolve, reject) => {
      this.client?.once(req.Tag, (ev: IRes) => {
        if (ev.Status == -1) {
          reject(ev.Errmsg);
        }
        resolve(ev.Result);
      });
    });
  }
}
